@extends("admin.layout.main")

@section("css")
    <link rel="stylesheet" href="/adstatic/zTree/css/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" href="/adstatic/rule/setRules/setRules.css" type="text/css">
@endsection

@section("content")
    <div class="admin-main layui-anim layui-anim-upbit">
        <fieldset class="layui-elem-field">
            <legend>配置权限</legend>
            <div class="layui-field-box">
                <form class="layui-form layui-form-pane">
                    <ul id="treeDemo" class="ztree"></ul>
                    <div class="layui-form-item text-center">
                        <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">提交</button>
                    </div>
                </form>
            </div>
        </fieldset>
    </div>
@endsection

@section("js")
    <script type="text/javascript" src="/adstatic/zTree/js/jquery.min.js"></script>
    <script type="text/javascript" src="/adstatic/zTree/js/jquery.ztree.core.min.js"></script>
    <script type="text/javascript" src="/adstatic/zTree/js/jquery.ztree.excheck.min.js"></script>
    <script type="text/javascript">
        var setting = {
            check:{enable: true},
            view: {showLine: false, showIcon: false, dblClickExpand: false},
            data: {
                simpleData: {enable: true, pIdKey:'pid', idKey:'id'},
                key:{name:'title'}
            }
        };
        var zNodes = {!!$rules!!};
        function setCheck() {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.setting.check.chkboxType = { "Y":"ps", "N":"ps"};

        }
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        setCheck();
        layui.use(['form', 'layer'], function () {
            var form = layui.form(), layer = parent.layer === undefined ? layui.layer : parent.layer;
            form.on('submit(submit)', function () {
                var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                // 提交到方法 默认为本身
                var treeObj = $.fn.zTree.getZTreeObj("treeDemo"),
                        nodes=treeObj.getCheckedNodes(true),
                        v=[];
                for(var i=0;i<nodes.length;i++){
                    v[i]=nodes[i].id;
                }
                var id = "{{$role_id}}";
                var token = '{{ csrf_token() }}';
                $.ajax({
                    url: '/setRules',
                    type: 'POST',
                    dataType: 'json',
                    timeout: 3000,
                    data: {id: id, rules: v, _token: token},
                    complete: function(){
                        top.layer.close(index);
                    },
                    error: function(){
                        layer.alert('参数错误', {icon: 2}, function(index){
                            layer.close(index);
                        });
                    },
                    success: function(res){
                        if (res.status != 0) {
                            top.layer.close(index);
                            layer.alert(res.msg, {icon: 2}, function (index) {
                                layer.close(index);
                            })
                            return false;
                        }
                        top.layer.msg("配置成功！");
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                    }
                });
            })
        });
    </script>
@endsection